package com.example.administrator.commonlibrary.bese;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.administrator.commonlibrary.R;
import com.example.administrator.commonlibrary.utils.ContexUtil;
import com.example.administrator.commonlibrary.utils.FragmentUtil;
import com.example.administrator.commonlibrary.utils.NoSlideColseActivityAddUtils;
import com.example.administrator.commonlibrary.utils.system.AppManager;
import com.jude.swipbackhelper.SwipeBackHelper;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.UMShareAPI;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import butterknife.ButterKnife;


public abstract class BaseActivity extends AppCompatActivity implements BaseContract.BaseView{


    protected boolean isActive;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(onLayout());
        initFontScale();
        //当Activity 被侧滑的时候finish（）
       if (NoSlideColseActivityAddUtils.getActivityColse(this))
           SwipeBackHelper.onCreate(this);
        //初始化 ButterKnife
        ButterKnife.bind(this);
        //实例化ARouter
       ARouter.getInstance().inject(this);
        //添加创建的Activity
        AppManager.getAppManager().addActivity(this);
    }
    protected abstract int onLayout();


   //设置 横竖屏
   @IntDef({ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
            ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
            ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT,
            ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Orientation {
    }
    @SuppressLint("WrongConstant")
    private void setScreenOrientation() {
        setRequestedOrientation(getScreenOrientation());
    }

    @Orientation
    protected abstract int getScreenOrientation();



    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if (NoSlideColseActivityAddUtils.getActivityColse(this))
            SwipeBackHelper.onPostCreate(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        isActive = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUMPageStart(setPageTitle());
    }

    @Override
    protected void onPause() {
        super.onPause();
        setUMPageEnd(setPageTitle());
    }

    @Override
    protected void onStop() {
        super.onStop();
        isActive = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (NoSlideColseActivityAddUtils.getActivityColse(this))
            SwipeBackHelper.onDestroy(this);
        UMShareAPI.get(this).release();//友盟分享防止内存泄漏
        //删除添加的Activity
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    //老人机  可以再 手机的设置里面设置字体大小，防止布局变形，让他对App不起作用
    private void initFontScale() {
        Configuration configuration = getResources().getConfiguration();
        configuration.fontScale = (float) 1;
        //0.85 小, 1 标准大小, 1.15 大，1.3 超大 ，1.45 特大
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.fontScale * metrics.density;
        getBaseContext().getResources().updateConfiguration(configuration, metrics);
    }
    /**
      * @desc 返回值：页面的标题
      * @param
      */
    protected abstract String setPageTitle();
    /**
      * @desc 友盟页面统计
      * @param title  页面的标题
      */
    @Override
    public void setUMPageStart(String title){
        boolean isEmpty = FragmentUtil.getFragmentList(getSupportFragmentManager());
        if (TextUtils.isEmpty(title)) throw new NullPointerException("友盟页面统计,setPageTitle不能为Null");
        if(isEmpty){
            MobclickAgent.onResume(this); //统计时长
        }else {
            MobclickAgent.onPageStart(title); //手动统计页面("SplashScreen"为页面名称，可自定义)
            MobclickAgent.onResume(this); //统计时长
        }
    }
    /**
     * @desc 友盟页面统计
     * @param title  页面的标题
     */
    @Override
    public void setUMPageEnd(String title){
        boolean isEmpty = FragmentUtil.getFragmentList(getSupportFragmentManager());
        if (TextUtils.isEmpty(title)) throw new NullPointerException("友盟页面统计,setPageTitle不能为Null");
        if(isEmpty){
            MobclickAgent.onPause(this); //统计时长
        }else {
            MobclickAgent.onPageEnd(title); //手动统计页面("SplashScreen"为页面名称，可自定义)
            MobclickAgent.onPause(this); //统计时长
        }

        }

    @Override
    public void setTheme(int resid) {
        super.setTheme(resid);
    }
}
